<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    title="编辑"
    :visible.sync="dialogVisible"
    width="70%"
  >
    <el-form
      ref="yaraForm"
      v-loading="loading"
      label-width="100px"
      :model="yaraForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="规则名" prop="name">
        <el-input
          v-model="yaraForm.name"
          placeholder="请输入规则名"
          spellcheck="false"
        />
      </el-form-item>

      <el-form-item label="规则描述" prop="abstract">
        <el-input
          v-model="yaraForm.abstract"
          :autosize="{ minRows: 3, maxRows: 20 }"
          spellcheck="false"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="yara" prop="yara">
        <el-input :autosize="{ minRows: 10, maxRows: 20 }" type="textarea" />
        <code-mirror v-model="yaraForm.yara" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('yaraForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import clearWatch from "@/utils/mixin/clearWatch";
import antivirus from "@/api/antivirus";
import CodeMirror from "@/components/module/codeMirror.vue";
export default {
  name: "EditYara",
  components: { CodeMirror },
  mixins: [clearWatch],
  props: {
    // eslint-disable-next-line vue/require-default-prop, vue/require-prop-type-constructor
    id: 0
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      yaraForm: {
        id: 0,
        yara: "",
        name: "",
        abstract: ""
      },
      yaraUnChanged: {
        id: 0,
        yara: "",
        name: "",
        abstract: ""
      },
      rules: {
        name: [{ required: true, message: "请输入规则名", trigger: "blur" }],
        yara: [{ required: true, message: "请输入yara", trigger: "blur" }],
        abstract: [
          { required: true, message: "请输入规则描述", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    show(yara) {
      this.dialogVisible = !this.dialogVisible;
      this.yaraForm = Object.assign({}, yara);
      this.yaraUnChanged = Object.assign({}, yara);
    },
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let datao = Object.assign({}, this.yaraForm);
          let yaraEdit = {};
          let nameEdit = {};
          let commentEdit = {};
          yaraEdit.yara_id = datao.id;
          nameEdit.yara_id = datao.id;
          commentEdit.yara_id = datao.id;
          nameEdit.yara_name = datao.name;
          commentEdit.abstract = datao.abstract;
          yaraEdit.yara = datao.yara;
          if (yaraEdit.yara != this.yaraUnChanged.yara)
            antivirus.edityara(yaraEdit).then(res => {
              this.$message.success(res.data.message);
            });
          if (commentEdit.abstract != this.yaraUnChanged.abstract)
            antivirus.editcomment(commentEdit).then(res => {
              this.$message.success(res.data.message);
            });
          if (nameEdit.yara_name != this.yaraUnChanged.name)
            antivirus.editname(nameEdit).then(res => {
              this.$message.success(res.data.message);
            });
          this.dialogVisible = false;
          this.$emit("change");
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped></style>
